package com.shujia.spark.core

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator

import java.lang

object Demo16Acc {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Demo15Cache")
    val sc = new SparkContext(conf)

    val linesRDD: RDD[String] = sc.textFile("data/students.txt", 2)

    var count = 0
    linesRDD.foreach(
      stu => {
        count += 1
        println(count)
      })
    println(count)

    //1、累加器
    //1、定义一个累加器
    val accCount: LongAccumulator = sc.longAccumulator
    linesRDD.foreach(stu => {
      //2、累加计算
      accCount.add(1)

    })
    //3、获取累加结果
    val value: lang.Long = accCount.value
    println(value)

    //count算子会启动一个新的job计算rdd的行数，而累加器不需要单独启动一个job
    val c: Long = linesRDD.count()
    println(c)

  }
}
